{% extends "index.twig" %}
{% set sstring = url_param('q','string' ) %}
{% block content %}

    <div>
        <blockquote class="small">The search function is primitive. It only searches for the literal string as entered. A search like <span>» fluffy cloud</span> will search only the exact string <span>» "fluffy cloud"</span>, and not <span>fluffy</span> AND/OR <span>cloud</span>, like search engines do.<br>Searches are case insensitive. Tags and some special characters are stripped.</blockquote>
    </div>

    {% set p = url_param('p','int',1) %}
    {# it would be nice if the search weren't repeated for each page, but this doesn't work:  #}
    {# {% if p == 1 %} #}
        {% set sstring = sstring|lower %}
        {% set results = {} %}
        {% for page in tbpages %}
            {% if sstring in page.id|content|striptags|lower or sstring in page.title|striptags|lower or sstring in page.id|lower %}
            {% set results = results|merge([page]) %}
            {% endif %}
        {% endfor %}
        {% set count = results|length %}
    {# {% endif %} #}

    <h4>{{ count }} search results for "{{sstring}}"</h4>

    {% if config.tagblog.paging > 0 and count > config.tagblog.paging %}
        {% set offset = (p - 1) * config.tagblog.paging %}
        {% set lastpage = count//config.tagblog.paging %}
        {% if count%config.tagblog.paging > 0 %}{% set lastpage = lastpage + 1 %}{% endif %}
        {% for page in results|slice(offset,config.tagblog.paging) %}
            {% include 'includes/postlistitem.twig' %}
        {% endfor %}
        <div class="pagination clearfix">
            {% if offset >= config.tagblog.paging %}
                <div class="previous"><a href="/search?q={{sstring}}&p={{ p - 1 }}">&lt;Previous Page</a></div>
            {% endif %}
            {# {% if offset >= config.tagblog.paging and offset + config.tagblog.paging < length %} | {% endif %} #}
            <div class="current">- {{ p }}/{{ lastpage }} -</div>
            {% if offset + config.tagblog.paging < count %}
                <div class="next"><a href="/search?q={{sstring}}&p={{ p + 1 }}">Next Page&gt;</a></div>
            {% endif %}
        </div>

    {% else %}
        {% for page in results %}
            {% include 'includes/postlistitem.twig' %}
        {% endfor %}
    {% endif %}

{% endblock content %}
